function AQIChange(url,num){
	getJSONData(url,function(data){
		if(data.Error==null){
			var time=[],aqi=[],pm=[],so=[],no=[],pm2=[],co2=[],o=[];
			var haqi=data.Data.reverse();
			var aqi2,pm25,so2,no2,pm10,co,o3;

			haqi.forEach(function(e,i){
			    aqi2=parseFloat(e.aqi);
				pm25=parseFloat(e.pm25);
				so2=parseFloat(e.so2);
				no2=parseFloat(e.no2);
				pm10=parseFloat(e.pm10);
				co=parseFloat(e.co);
				o3=parseFloat(e.o3);
				var aqiitem={
					color: num2color(aqi2),
					y: aqi2
					,marker:i==0?{width:15,height:15,symbol:'url(./assets/img/num-1.png)'}:null
				}			
				var pmitem={
					color: num2color(pm25),
					y: pm25
					,marker:i==0?{width:15,height:15,symbol:'url(./assets/img/num-2.png)'}:null
				}
				var soitem={
					color: num2color(so2),
					y: so2
					,marker:i==0?{width:15,height:15,symbol:'url(./assets/img/num-3.png)'}:null
				}	
				var noitem={
					color: num2color(no2),
					y: no2
					,marker:i==0?{width:15,height:15,symbol:'url(./assets/img/num-4.png)'}:null
				}
				var pm2item={
					color: num2color(pm10),
					y: pm10
					,marker:i==0?{width:15,height:15,symbol:'url(./assets/img/num-5.png)'}:null
				}
				var coitem={
					color: num2color(co),
					y: co
					,marker:i==0?{width:15,height:15,symbol:'url(./assets/img/num-6.png)'}:null
				}
				var oitem={
					color: num2color(o3),
					y: o3
					,marker:i==0?{width:15,height:15,symbol:'url(./assets/img/num-7.png)'}:null
				}				
				time.push(e.date);
				aqi.push(aqiitem);
				pm.push(pmitem);
				so.push(soitem);
				no.push(noitem);
				pm2.push(pm2item);
				co2.push(coitem);
				o.push(oitem);
			});
			if (num==0){
			    AQIChart(time,aqi,pm,so,no,pm2,co2,o);
			}
			//缓存
			AQIChartCache[num]={time:time,aqi:aqi,pm:pm,so:so,no:no,pm2:pm2,co:co2,o:o};
		}
	});	
}

//折线图
function AQIChart(time,aqi,pm,so,no,pm2,co,o){
	var chart1 = new Highcharts.Chart({
	    chart: {
	        renderTo: 'aqi-chart'
	    },
		// $('#aqi-chart').highcharts({
			credits:{
				enabled:false
			},
	    title: {
	        text: '',
	        x: -20 //center
	    },
	    subtitle: {
	        text: '指数',
			align: 'left'			
	    },
	    xAxis: {
	        categories: time,
	        gridLineColor:'#EEEEEE',
	        gridLineWidth: 1,
	        tickLength:3,
	        labels:{
	        	enabled:true,
	        	style:{
	        		color: '#A7A7A7',
					fontWeight: 'normal',
				    fontSize:'8px'
	        	}
	        }
	    },
	    yAxis: {
	        title: {
	            text: ''
	        },
	        gridLineWidth: 0,
	        labels:{
	        	style:{
	        		color: '#B7B7B7',
					fontWeight: 'normal',
					fontSize:'8px'
	        	}
	        }
	    },
	    tooltip: {
	        // valueSuffix: 'ug/m³'
	        formatter:function(){
	        	var suffix='',name=this.series.name;
	        	return '<small>'+this.x+'</small><br/>'+name+': '+this.y+suffix;
	        }
	    },
	    legend: {
	    	enabled:false,
	        layout: 'horizontal',
	        align: 'right',
	        verticalAlign: 'top',
	        borderWidth: 0,
	        lineHeight:12,
	        margin:0,
	        padding:0,
	        itemMarginBottom:8,
	        itemStyle:{
	        	fontWeight:"normal",
	        	fontSize:'8px'
	        }
	    },
	    plotOptions: {
	        series: {
	            marker: {
	                fillColor: null,
	                lineWidth: 1,
	                radius: 3.5,
	                lineColor: null // inherit from series
	                ,states:{
	                	hover:{
	                		enabled:true,
	                		fillColor: null,
	                		lineColor: null
	                	},
	                	select:{
	                		enabled:true,
	                		fillColor: null,
	                		lineColor: null
	                	}
	                }
	            }
	        }
	    },
	    series: [{
	        name: 'AQI',
	        data: aqi,
	        color:'#FE9A01'
	    },{
	        name: 'PM2.5',
	        data: pm,
	        color:'#00E3D8',
			visible:false
	    }, {
	        name: 'so2',
	        data: so,
	        color:'#e4b503',
			visible:false
	    }, {
	        name: 'no2',
	        data: no,
	        color:'#688ff4',
			visible:false
	    }, {
	        name: 'pm10',
	        data: pm2,
	        color:'#23ae39',
			visible:false
	    }, {
	        name: 'co',
	        data: co,
	        color:'#e16f6e',
			visible:false
	    }, {
	        name: 'o3',
	        data: o,
	        color:'#01b3d9',
			visible:false
	    }]
    });
	drawMyLegend(chart1);
}

/**
 *遍历自定义折线图图标状态
 */ 
function drawMyLegend(chart) {
    $('.zb-item').each(function(i) {
        //if (chart.series[i + 2] == undefined) return;
        var disable = $(this).hasClass('active');
        if (disable == true) {
            chart.series[i].show();
        } else {
            chart.series[i].hide();
        }
    });
}

//天气预报
function findWeather(url) {
	getJSONData(url,function(data){
		if(data.Error==null){
			var weather=data.Data;
			$('.pb-weather').text(weather.temperature+'℃ / '+weather.weather+' / '+weather.wind.dir+weather.wind.sc+'级');
		}
	});	   
}

//省内及长三角排名方法
function get_citys_order(){
	var jsorder=[],csjorder=[],jstop,csjtop;
	var JSCitys  = '盐城市,南通市,扬州市,泰州市,镇江市,宿迁市,苏州市,淮安市,常州市,连云港市,无锡市,南京市,徐州市';
	var CSJCitys = '拉萨,海口,昆明,惠州,厦门,舟山,张家口,福州,深圳,中山,珠海,大连,台州,肇庆,贵阳,南昌,佛山,东莞,广州,丽水,承德,南宁,江门,盐城,衢州,西宁,温州,合肥,上海,淮安,宁波,扬州,泰州,宿迁 ,连云港,兰州,镇江,青岛,杭州,嘉兴,湖州,绍兴,南通,呼和浩特,苏州,无锡,南京,北京,金华,长沙,常州,秦皇岛,廊坊,重庆,徐州,西安,长春,乌鲁木齐,沧州,成都,武汉,天津,太原 ,唐山,银川,哈尔滨,济南,沈阳,衡水,郑州,邯郸,石家庄,邢台,保定';
	getJSONData(RequestUrl+'/map',function(data){
		data.Data.forEach(function(e){
			if(JSCitys.indexOf(e.name)>-1&&e.name!='南京市'){
				jsorder.push({name:e.name,aqinum:e.aqi});
			}
			if(CSJCitys.indexOf(RMS(e.name))>-1&&e.name!='南京市'){
				csjorder.push({name:e.name,aqinum:e.aqi});
			}
		});
		getJSONData(RequestUrl+'/data?act=aqin&areacode=2000',function(obj){
			var hisdata = obj.Data;
			// for(var i in hisdata){
			// 	if(hisdata[i].name=='市区'){
					jsorder.push({name:'南京市',aqinum:hisdata[0].aqi});
					csjorder.push({name:'南京市',aqinum:hisdata[0].aqi});
			// 	}
			// }
			jsorder=jsorder.sort(function(a,b){
				var i=-1,an=parseInt(a.aqinum),bn=parseInt(b.aqinum);
				if(an==bn){
					if(RMS(a.name)=='南京市') i=-1;
					if(RMS(b.name)=='南京市') i=1;
				}else{
					i=an-bn;
				}
				return i
			});
			// csjorder=[].concat(csjorder,jsorder);
			csjorder.sort(function(a,b){
				var i=-1,an=parseInt(a.aqinum),bn=parseInt(b.aqinum);
				if(an==bn){
					if(RMS(a.name)=='南京市') i=-1;
					if(RMS(b.name)=='南京市') i=1;
				}else{
					i=an-bn;
				}
				return i
			});
			$.each(jsorder, function(i,e) {
				if(e.name=='南京市'){
					jstop = i+1;
				}
			});
			$.each(csjorder, function(i,e) {
				if(e.name=='南京市'){
					csjtop = i+1;
				}
			});	
			$('#js-order-val').html(jstop);
			$('#csj-order-val').html(csjtop);
		});
	});
}
	
//获取74城市省内及长三角排名
function citys74(month){
	if(month==undefined){
		var date=new Date(),year=date.getFullYear(),month=date.getMonth()-1;
		if(month==0){
			year=year-1;
			month=12;
		}
		if(year==2016&&month>3) month=3;
		month=(month<=9?'0'+month:month);
		month=year+''+month;
	}
	getJSONData('./xlsx?act=order&month='+month,function(data){
		if(data.Error==null){
			var all=data.Data.all,js=data.Data.js;
			$('#citys74-js-order-val').html(js);
			$('#citys74-all-order-val').html(all);
		}
	});
	$('#month-74citys-select').val(month);
}
	

function getAQIItemByCode(code){
	var codeitems={
		211:'SO<sub>2</sub>',
		213:'NO<sub>2</sub>',
		215:'CO',
		218:'O<sub>3</sub>',
		220:'PM<sub>10</sub>',
		221:'PM<sub>2.5</sub>',
		2151:'CO'
	};
	return codeitems[code];
}
function num2color(num){
	var num=parseInt(num);
	var color='transparent';
	if(num<50){
		color='#23C700';
	}else if(50<num&&num<=100){
		color='#ECD814';
	}else if(100<num&&num<=150){
		color='#FF9D01';
	}else if(150<num&&num<=200){
		color='#F83702';
	}else if(200<num&&num<=300){
		color='#CD162E';
	}else if(num>300){
		color='#8E0C30';
	}
	return color
}
function num2imgbg(num){
	var num=parseInt(num);
	var color='_23C700';
	if(num<50){
		color='_23C700';
	}else if(50<num&&num<=100){
		color='_ECD814';
	}else if(100<num&&num<=150){
		color='_FF9D01';
	}else if(150<num&&num<=200){
		color='_F83702';
	}else if(200<num&&num<=300){
		color='_CD162E';
	}else if(num>300){
		color='_8E0C30';
	}
	return color
}

function num2table(num){
	var num=parseInt(num);
	var idx=0,note='';
	if(num<50){
		idx=0;
		note='优秀';
	}else if(50<=num&&num<=100){
		idx=1;
		note='良好';
	}else if(100<num&&num<=150){
		idx=2;
		note='轻度';
	}else if(150<num&&num<=200){
		idx=3;
		note='中度';
	}else if(200<num&&num<=300){
		idx=4;
		note='重度';
	}else if(num>300){
		idx=5;
		note='严重';
	}
	return {idx:idx,note:note}
}

function note2num(note){
	var num=0;
	if(note.indexOf('良')>-1){
		num=51
	}else if(note.indexOf('轻度')>-1){
		num=101
	}else if(note.indexOf('中度')>-1){
		num=151
	}else if(note.indexOf('重度')>-1){
		num=201
	}else if(note.indexOf('严重')>-1){
		num=301
	}
	return num
}

//通用方法
function getJSONData(url,callback){
  $.ajax({
      type: "get",
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      url: url,  //这里是网址
      success: function (json) {
          callback(json)
      },
      timeout: 60000,
      error: function (XMLHttpRequest, textStatus, errorThrown) {
          console.error(url,textStatus+": "+errorThrown);
      }
  });
}

//获取页面的高度、宽度
function getPageSize() {
    var xScroll, yScroll;
    if (window.innerHeight && window.scrollMaxY) {
        xScroll = window.innerWidth + window.scrollMaxX;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else {
        if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac    
            xScroll = document.body.scrollWidth;
            yScroll = document.body.scrollHeight;
        } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari    
            xScroll = document.body.offsetWidth;
            yScroll = document.body.offsetHeight;
        }
    }
    var windowWidth, windowHeight;
    if (self.innerHeight) { // all except Explorer    
        if (document.documentElement.clientWidth) {
            windowWidth = document.documentElement.clientWidth;
        } else {
            windowWidth = self.innerWidth;
        }
        windowHeight = self.innerHeight;
    } else {
        if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode    
            windowWidth = document.documentElement.clientWidth;
            windowHeight = document.documentElement.clientHeight;
        } else {
            if (document.body) { // other Explorers    
                windowWidth = document.body.clientWidth;
                windowHeight = document.body.clientHeight;
            }
        }
    }       
    // for small pages with total height less then height of the viewport    
    if (yScroll < windowHeight) {
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }    
    // for small pages with total width less then width of the viewport    
    if (xScroll < windowWidth) {
        pageWidth = xScroll;
    } else {
        pageWidth = windowWidth;
    }
    arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight);
    return arrayPageSize;
}

/**
 * 补零函数 向左补零 取两位
 * @param {String} str
 */
function appendZeroleft(str){
	return ('0'+str).substr(-2,2);
}

function RMS(s){
  return s.replace('市','');
}